﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Truextend.Training.Business.Interfaces;
using Truextend.Training.Business.Dtos;
using Truextend.Training.Model.Entities;
using Truextend.Common.Data;
using Microsoft.SharePoint;

namespace Truextend.Training.Business
{
    public class CourseInstanceManager : Repository<CourseInstance>, ICourseInstanceManager
    {
        public CourseInstanceManager(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        }

        public IList<CourseInstance> GetAllCourseInstancesAvailable()
        {
            var context = (TrainingEntities)Context;
            var info = from a in context.CourseInstances.Include("Course").Include("ScheduleCourses")
                       where a.StartDate >= DateTime.Today
                       select a;
            
            return info.ToList();
        }
        
        public CourseInstance GetCourseInstanceById(long courseInstanceId)
        {
            var context = (TrainingEntities)Context;
            CourseInstance courseInstance = context.CourseInstances.Include("Course").Include("ScheduleCourses").First(i => i.CourseInstanceId == courseInstanceId);
            return courseInstance;
        }


        public CourseInstance AddCourseInstance(CourseInstance courseInstance)
        {
            this.Add(courseInstance);
            return courseInstance;
        }

        public CourseInstance EditCourseInstance(CourseInstance courseInstance)
        {
            CourseInstance courseInstanceEdit = GetCourseInstanceById(courseInstance.CourseInstanceId);
            courseInstanceEdit.Location = courseInstance.Location;
            courseInstanceEdit.StartDate = courseInstance.StartDate;
            courseInstanceEdit.EndDate = courseInstance.EndDate;
            courseInstanceEdit.InstructorId = courseInstance.InstructorId;
            courseInstanceEdit.State = courseInstance.State;

            // delete old SchelduleCourse list
            List<ScheduleCourse> deleteList = new List<ScheduleCourse>();
            foreach (ScheduleCourse sc in courseInstanceEdit.ScheduleCourses)
            {
                deleteList.Add(sc);
            }
            
            foreach (ScheduleCourse sc in deleteList)
            {
                this.Context.DeleteObject(sc);
            }
            // Add new SchelduleCourse list
            List<ScheduleCourse> newList = new List<ScheduleCourse>();
            foreach (ScheduleCourse sc in courseInstance.ScheduleCourses)
            {
                newList.Add(sc);
            }

            foreach (ScheduleCourse sc in newList)
            {
                courseInstanceEdit.ScheduleCourses.Add(sc);
            }
                        
            return courseInstance;
        }

        public CourseInstance DeleteCourseInstance(CourseInstance courseInstance)
        {
            throw new NotImplementedException();
        }

        public IList<CourseInstance> GetInstancesOneDayBeforeStart()
        {
            var context = (TrainingEntities)Context;
            DateTime tomorrow = DateTime.Today.AddDays(1);
            var instances = from a in context.CourseInstances
                       where a.StartDate <= tomorrow && a.State == "AP" //Approved
                       select a;

            foreach (var i in instances)
            {
                i.State = "EP"; //Email Pending
            }

            return instances.ToList();
        }
    }
}
